package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-02-20
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> treeSelect(Long id) {
//        根据id查询包含子分类
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, id);
        List<Category> list = baseMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(list)){
            for (Category category : list) {
                Long count = baseMapper.selectCount(new LambdaQueryWrapper<Category>().eq(Category::getParentId, category.getId()));
                category.setHasChildren(count > 0);
            }
        }
        return list;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {
        //查询所有一级分类
        List<Category> allCategoryList = baseMapper.selectList(
                new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0)
        );
        //转换为vo
        return allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<CategoryVo> tree() {
        //查询所有分类
        List<Category> allCategoryList = baseMapper.selectList(null);
        //转换为vo
        List<CategoryVo> categoryVoList = allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        //转换为树形结构
        return buildTree(categoryVoList);
    }

    /**
     * 使用递归方法建分类树
     * @param categoryVoList
     * @return
     */
    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        //创建树形结构
        List<CategoryVo> trees = new ArrayList<>();
        //遍历所有节点
        for (CategoryVo categoryVo : categoryVoList) {
            //判断是否是根节点
            if (categoryVo.getParentId().longValue() == 0) {
                //查找当前节点的子节点，添加到树形结构中
                categoryVo = findAndAddChildren(categoryVo, categoryVoList);
                trees.add(categoryVo);
            }
        }
        return trees;
    }

    /**
     * 在categoryVoList中找到categoryVo的子节点，并组装到categoryVo中
     * @param categoryVo
     * @param categoryVoList
     * @return 返回组装好的categoryVo节点
     */
    private CategoryVo findAndAddChildren(CategoryVo categoryVo, List<CategoryVo> categoryVoList) {
        //遍历所有节点
        for (CategoryVo node : categoryVoList) {
            //判断是否是categoryVo的子节点
            if(node.getParentId().longValue() == categoryVo.getId().longValue()) {
                //在categoryVoList中找到categoryVo的子节点，
                CategoryVo children = findAndAddChildren(node, categoryVoList);
                //并组装到categoryVo中
                categoryVo.getChildren().add(children);
            }
        }
        return categoryVo;
    }
}
